
# -*- coding: utf-8 -*-

from django.db import models
from comunidadcyt.middleware import get_request
from django.utils.encoding import force_unicode
import unicodedata

class Partners(models.Model):
    Nombre_partner = models.CharField(max_length=200, verbose_name="* Nombre", unique=True)
    Descripcion_partner = models.TextField(blank=True, verbose_name="Descripción") 
    Fecha_asociacion = models.DateField(verbose_name="* Fecha de asociación")
    PaginaWeb = models.URLField(max_length=300,blank=True,null=True, verbose_name="Página web")
    CV= models.FileField(max_length=300,upload_to='media/Partners', blank=True,editable=False)
    Imagen = models.ImageField(max_length=300,upload_to='media/Partners',blank=True,null=True,default="static/images/default.png", verbose_name="Logo")
    Creador = models.CharField(max_length=200,editable=False)
    
    def save(self, *args):
        self.Creador=get_request().user.username
        for field in self._meta.fields:
            if field.name == 'Imagen':
                field.upload_to = 'media/Partners/%s' % 'proyectos/%s' % unicodedata.normalize('NFKD', self.Nombre_partner).encode('ascii','ignore').replace(" ", "")
            if field.name == 'CV':
                field.upload_to = 'media/Partners/%s' % 'proyectos/%s' % unicodedata.normalize('NFKD', self.Nombre_partner).encode('ascii','ignore').replace(" ", "")
        super(Partners, self).save() 
    
    def get_absolute_url(self):
        return '/comunidadcyt/partners/ver/%i' % self.id
    
    class Meta:
        db_table = 'Partners' 
        ordering = ['Nombre_partner']
        verbose_name = "Partner"
        
       
    def __str__(self):
        return force_unicode(self.Nombre_partner)

    def __unicode__(self):
        return force_unicode(self.Nombre_partner)